use crate::util::encryption;
use std::os::unix::fs::PermissionsExt;


/**
 * 隔离文件
 */
pub fn isolate(src_path: &str, isolate_path: &str) {
    let key: [u8; 32] = [0x03, 0x23, 0x8e, 0x05, 0xba, 0xba, 0x78, 0x03, 0xab, 0x01, 0x12, 0x09, 0xac, 0x33, 0x23, 0x9b, 0x12, 0x01, 0x56, 0x8e, 0x12, 0xe2, 0xa9, 0x1e, 0x46, 0xa3, 0x21, 0x1f, 0x98, 0xaa, 0x88, 0x89];
    let nonce: [u8; 19] = [0x23, 0x8e, 0x03, 0xab, 0x01, 0x09, 0xac, 0x33, 0x01, 0x56, 0x8e, 0x12, 0xe2, 0xa9, 0x46, 0xa3, 0x1f, 0x98, 0xaa];
    let result = encryption::encrypt_large_file(src_path, isolate_path, &key, &nonce);
    if result.is_err() {
        println!("{:?}", result);
    } else {
        let result = std::fs::remove_file(src_path);
        if result.is_err() {
            println!("{:?}", result);
        }
    }
}

/**
 * 恢复文件
 */
pub fn recovery(isolate_path: &str, src_path: &str, mode: u32) {
    let key: [u8; 32] = [0x03, 0x23, 0x8e, 0x05, 0xba, 0xba, 0x78, 0x03, 0xab, 0x01, 0x12, 0x09, 0xac, 0x33, 0x23, 0x9b, 0x12, 0x01, 0x56, 0x8e, 0x12, 0xe2, 0xa9, 0x1e, 0x46, 0xa3, 0x21, 0x1f, 0x98, 0xaa, 0x88, 0x89];
    let nonce: [u8; 19] = [0x23, 0x8e, 0x03, 0xab, 0x01, 0x09, 0xac, 0x33, 0x01, 0x56, 0x8e, 0x12, 0xe2, 0xa9, 0x46, 0xa3, 0x1f, 0x98, 0xaa];
    let result = encryption::decrypt_large_file(isolate_path, src_path, &key, &nonce);
    if result.is_err() {
        println!("{:?}", result);
    } else {
        let result = std::fs::set_permissions(src_path, std::fs::Permissions::from_mode(mode));
        if result.is_err() {
            println!("{:?}", result);
        }
        let result = std::fs::remove_file(isolate_path);
        if result.is_err() {
            println!("{:?}", result);
        }
    }
}